home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
system
/
epuptm17.zip
/
UPTIME.C
< prev
next >
Wrap
C/C++ Source or Header
|
1994-05-15
|
10KB
|
339 lines
#ifndef __TINY__
#error Use TINY memory model--Don't forget to turn off debug info
#endif
#include <stdio.h>
#include <dos.h>
#include <dir.h>
#include <time.h>
#include <string.h>
#include <bios.h>
#include <sys\stat.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#define VER "v1.7"
#define RIGHT 0x01
#define LEFT 0x02
#define TRUE 1
#define FALSE 0
#define NUMTAIL 20
void
touch(char[]);
void
main(void);
char *
upit(char[]);
void
writeout(void);
void
help(void);
void
printlog(void);
void
add2log(void);
void
getlog(char *);
void
tail(void);
char filename[255], bday[4], bmon[4], ampm[3];
char set = FALSE, log = FALSE, needhelp = FALSE, cnt, found,showtail = FALSE;
struct ffblk ffblk;
struct time curtime;
struct date curdate;
struct tm old, *new;
time_t thetime;
int sec, min, day, hour, bdate, bhour, bmin, bsec, byear;
struct COUNTRY cinfo;
void
main(void)
{ strcpy(ampm,"am");
strcpy(filename, searchpath(_argv[0]));
country(0,&cinfo);
for (cnt = 1; cnt + 1 <= _argc; cnt++) {
found = FALSE;
if (strcmp("SET", upit(_argv[cnt])) == 0)
set = found = TRUE;
if (strcmp("LOG", upit(_argv[cnt])) == 0)
log = found = TRUE;
if (strcmp("TAIL",upit(_argv[cnt])) == 0)
showtail = found = TRUE;
if (!found) {
needhelp = TRUE;
break;
}
}
if (needhelp) {
help();
exit(1);
}
if (set)
touch(filename);
if (log){
if (set)
add2log();
if (!set&&!showtail)
printlog();
if (!set&&showtail){
printf("Error: Cannot combine \"log\" and \"tail\" without \"set\"\n");
exit(1);
}
}
if (set) //&&!showtail)
puts("...");
if (showtail){
if (set)
printf("\n");
tail();
}
if (_argc==1)
writeout();
fcloseall();
} /* main */
void
touch(char name[])
{
FILE *file;
char tmp;
int modifiers;
modifiers = bioskey(2);
if (!(modifiers & RIGHT || modifiers & LEFT)) {
printf("Updating launch time");
file = fopen(name, "r+");
fseek(file, SEEK_SET, 0);
fread(&tmp, 1, 1, file);
rewind(file);
fwrite(&tmp, 1, 1, file);
} else {
printf("Aborting update of %s\n", _argv[0]);
exit(1);
}
} /* touch */
char *
upit(char str[])
{
int i;
for (i = 0; i < strlen(str); i++) {
str[i] = toupper(str[i]);
}
return str;
}
void
writeout(void)
{
char tz[257], *oldtz;
tz[0] = '\0';
oldtz = getenv("TZ");
if (oldtz != NULL) {
strcat(tz, "TZ=");
putenv(tz);
}
findfirst(filename, &ffblk, FA_DIREC);
old.tm_sec = 2 * (ffblk.ff_ftime & 31);
old.tm_min = (ffblk.ff_ftime & 2016) >> 5;
old.tm_hour = (ffblk.ff_ftime & 63488) >> 11;
old.tm_mday = (ffblk.ff_fdate & 31);
old.tm_mon = ((ffblk.ff_fdate & 480) >> 5) - 1;
old.tm_year = ((ffblk.ff_fdate & 65024) >> 9) + 80;
mktime(&old);
thetime = time(NULL);
new = localtime(&thetime);
sec = (*new).tm_sec - old.tm_sec;
min = (*new).tm_min - old.tm_min;
if (sec < 0) {
sec += 60;
min--;
}
hour = (*new).tm_hour - old.tm_hour;
if (min < 0) {
min += 60;
hour--;
}
day = (*new).tm_yday - old.tm_yday + (365 * ((*new).tm_year - old.tm_year));
if (hour < 0) {
hour += 24;
day--;
}
sscanf(asctime(&old), "%3s %3s %d %d:%d:%d %d", bday, bmon, &bdate, &bhour, &bmin, &bsec, &byear);
if (bhour >= 12&&cinfo.co_time==0) {
bhour -= 12;
ampm[0] = 'p';
}// else
// ampm[0] = '\0';
if (bhour == 0&&cinfo.co_time==0)
bhour = 12;
if (day < 0)
printf("Error: System clock has been changed since bootup\n\n");
else if (day != 0)
switch (cinfo.co_date){
case 0:printf("System launched at %2.2d%c%2.2d%c%2.2d%s on %s, %s %d, %d\nUp for %d day(s), %2.2d%c%2.2d%c%2.2d\n", bhour,cinfo.co_tmsep[0], bmin,cinfo.co_tmsep[0], bsec, ampm, bday, bmon, bdate, byear, day, hour,cinfo.co_tmsep[0], min,cinfo.co_tmsep[0], sec);
break;
case 1:printf("System launched at %2.2d%c%2.2d%c%2.2d on %s, %d %s, %d\nUp for %d day(s), %2.2d%c%2.2d%c%2.2d\n", bhour,cinfo.co_tmsep[0], bmin,cinfo.co_tmsep[0], bsec, bday, bdate, bmon, byear, day, hour,cinfo.co_tmsep[0], min,cinfo.co_tmsep[0], sec);
break;
case 2:printf("System launched at %2.2d%c%2.2d%c%2.2d on %s, %d %s, %d\nUp for %d day(s), %2.2d%c%2.2d%c%2.2d\n", bhour,cinfo.co_tmsep[0], bmin,cinfo.co_tmsep[0], bsec, bday, byear, bmon, bdate, day, hour,cinfo.co_tmsep[0], min,cinfo.co_tmsep[0], sec);
break;
}
else
switch (cinfo.co_date){
case 0:printf("System launched at %2.2d%c%2.2d%c%2.2d%s on %s, %s %d, %d\nUp for %2.2d%c%2.2d%c%2.2d\n", bhour,cinfo.co_tmsep[0], bmin,cinfo.co_tmsep[0], bsec, ampm, bday, bmon, bdate, byear, hour,cinfo.co_tmsep[0], min,cinfo.co_tmsep[0], sec);
break;
case 1:printf("System launched at %2.2d%c%2.2d%c%2.2d on %s, %d %s, %d\nUp for %2.2d%c%2.2d%c%2.2d\n", bhour,cinfo.co_tmsep[0], bmin,cinfo.co_tmsep[0], bsec, bday, bdate, bmon, byear, hour,cinfo.co_tmsep[0], min,cinfo.co_tmsep[0], sec);
break;
case 2:printf("System launched at %2.2d%c%2.2d%c%2.2d on %s, %d %s, %d\nUp for %2.2d%c%2.2d%c%2.2d\n", bhour,cinfo.co_tmsep[0], bmin,cinfo.co_tmsep[0], bsec, bday, byear, bmon, bdate, hour,cinfo.co_tmsep[0], min,cinfo.co_tmsep[0], sec);
break;
}
if (oldtz != NULL) {
strcpy(tz, oldtz);
putenv(tz);
}
}
void
help(void)
{
printf("\nUptime %s by Eric S. Peters\n", VER);
printf("Compiled at %s, on %s\n\n", __TIME__, __DATE__);
puts("Uptime will tell you how long your computer has been running since the last");
puts("time it was launched. Executed with no parameters, it returns the time elapsed");
puts("since bootup.\n");
puts("SET\t: Will set the launchtime. This needs to be in the AUTOEXEC.BAT file.");
puts("\t: If either shift key is held, the \"set\" is aborted.");
puts("LOG\t: Will print the log, or add to the log if called with \"set\".");
puts("\t: The log file will be in the same directory as uptime.");
puts("TAIL\t: Will print the last 20 entries in the log.");
puts("\t: Can be combined with \"set\" with no problem.");
puts("\t: Can only be combined with \"log\" if \"set\" is called as well.");
puts("\nUptime can be copied and distributed freely.\n");
puts("I used to think that it would be pretty hard to find a bug in a small program");
puts("like this, but I was wrong. I can be reached at peters@cs.colostate.edu.\n");
}
void
printlog()
{
char logname[255], line[81], ans, tail[NUMTAIL][81];
FILE *log;
int cnt = 0,num2prn=0;
getlog(logname);
log = fopen(logname, "r");
if (log != NULL) {
while (fgets(line, 81, log) != NULL) {
printf("%s", line);
if (++cnt % 24 == 0 && eof(log) != 0) {
printf("(T)ail, (Q)uit, or anything else\r");
ans = toupper(getch());
while(kbhit())
getch();
switch (ans) {
case 'Q':
case 27:
exit(0);
break;
case 'T':
cnt = 0;
fseek(log, 0, SEEK_SET);
while (fgets(tail[cnt++ % NUMTAIL], 81, log) != NULL);
num2prn=(--cnt);
if (num2prn>20)
clrscr();
if (num2prn>NUMTAIL)
num2prn=NUMTAIL;
printf("Last %d Launch(es):\n\n", num2prn);
for (ans = 1; ans <= num2prn;